<?php
declare (strict_types=1);

namespace app\user\service\teacher;

use app\common\exception\LogicException;
use app\common\logic\category\Category;
use app\common\service\BaseService;
use app\course\logic\base\Course;
use app\course\logic\base\CourseSection;
use app\user\logic\teacher\Teacher as TeacherLogic;
use app\user\logic\teacher\TeacherCourse as TeacherCourseLogic;
use think\facade\Request;

/**
 * 讲师课程接口类
 */
class TeacherCourse extends BaseService
{

    // 获取讲师授课
    public function getTeacherCourse(string $type)
    {
        $type_arr = [
            'video'   => 1,
            'live'    => 2,
            'offline' => 3,
            'all'     => '1,2,3'
        ];
        if (!in_array($type, array_keys($type_arr))) throw new LogicException("请求接口失败");

        // $mid        = request()->mid;
        $teacher_id = app(TeacherLogic::class)->scope('mid')->value('id');
        $map[]      = ['teacher_id', '=', $teacher_id];

        $eq    = is_string($type_arr[$type]) ? 'IN' : '=';
        $map[] = ['course_type', $eq, $type_arr[$type]];

        input('?get.status') && $map[] = ['status', '=', input('get.status/d')];
        input('?get.title') && $map[] = ['title', 'LIKE', '%' . input('get.title/s') . '%'];

        // 面授课参数，暂不使用，合作课程或独立课程
        // if ( input('?get.author') ) {
        //     if ( input('get.author/d') ) {
        //         $map[] = ['user_id', '=', $mid];
        //     } else {
        //         $map[] = ['user_id', '<>', $mid];
        //     }
        // }

        $data = app(TeacherCourseLogic::class)->getTeacherCourse($map);

        return $this->parseToData($data, 1, '获取成功');
    }

    // 上传课程
    public function addCourse()
    {
        $param = Request::post();
        $data  = app(TeacherCourseLogic::class)->addCourse($param);

        return $this->parseToData($data, 1, '上传成功');
    }

    // 编辑课程
    public function editCourse(int $id)
    {
        if (Request::isPut()) {
            $param = Request::put();
            $data  = app(TeacherCourseLogic::class)->editCourse($id, $param);

            return $this->parseToData($data, 1, '编辑成功');
        }

        $field = 'id,title,course_type,teacher_id,user_id,mhm_id,category,fullcategorypath,mhm_category,mhm_fullcategorypath,cover,info,price,scribing_price,listing_time,section_level,course_live_type,limit_buy_count';

        $data = app(Course::class)->with(['cover_url'])->field($field)->findOrEmpty($id)->hidden(['mhm_category', 'mhm_fullcategorypath']);
        !is_array($data->fullcategorypath) && $data->fullcategorypath = app(Category::class)->cateStrToArr($data->fullcategorypath);

        if ($data->mhm_id != 1) {
            $data->fullcategorypath = $data->mhm_fullcategorypath;
            $data->category         = $data->mhm_category;
        }

        return $this->parseToData($data, 1, '编辑成功');
    }

    /**
     * 完结课时
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-21
     *
     * @param int $id [description]
     * @return [type]     [description]
     */
    public function finishCourse(int $id)
    {
        app(TeacherCourseLogic::class)->finishCourse($id);

        return $this->parseToData(null, 1, '完结成功');
    }

    // 关闭课程
    public function closeCourse(int $id)
    {
        $data         = app(Course::class)->findOrFail($id);
        $data->status = 0;
        if (!$data->save()) {
            throw new LogicException("关闭失败");
        }

        return $this->parseToData([], 1, '关闭成功');
    }

    // 开启课程
    public function openCourse(int $id)
    {
        $data         = app(Course::class)->findOrFail($id);
        $data->status = 1;
        if (!$data->save()) {
            throw new LogicException("开启失败");
        }

        return $this->parseToData([], 1, '开启成功');
    }

    // 删除课程
    public function deleteCourse(int $id)
    {
        $data = app(Course::class)->findOrEmpty($id);
        $data->delete();

        return $this->parseToData([], 1, '删除成功');
    }

    public function getCourseSectionTree($id)
    {
        $data = app(CourseSection::class)->getCourseSectionTree($id);

        return $this->parseToData($data, 1, '获取成功');
    }

    public function getCourseSectionLevel($id)
    {
        $data = app(CourseSection::class)->getCourseSectionLevel($id);

        return $this->parseToData($data, 1, '获取成功');
    }

    // 添加章节
    public function addCourseSectionMenu(int $id, int $pid)
    {
        $title = Request::post('title/s');
        $data  = app(CourseSection::class)->addCourseSectionMenu($id, $pid, $title);

        return $this->parseToData($data, 1, '添加成功');
    }

    // 添加课时
    public function addCourseSectionData(int $id, int $pid)
    {
        $param = Request::post();
        $data  = app(CourseSection::class)->addCourseSectionData($id, $pid, $param);

        return $this->parseToData($data, 1, '添加成功');
    }

    // 编辑章节目录
    public function editCourseSectionMenu(int $id, int $sid)
    {
        $title = Request::put('title/s');
        $data  = app(CourseSection::class)->editCourseSectionMenu($id, $sid, $title);

        return $this->parseToData($data, 1, '编辑成功');
    }

    // 编辑课时获取原数据
    public function getCourseSectionData(int $id, int $sid)
    {
        $data = app(CourseSection::class)->getCourseSectionData($id, $sid);

        return $this->parseToData($data, 1, '获取成功');
    }

    // 编辑课时
    public function editCourseSectionData(int $id, int $sid)
    {
        $param = Request::put();
        $data  = app(CourseSection::class)->editCourseSectionData($id, $sid, $param);

        return $this->parseToData($data, 1, '编辑成功');
    }

    // 删除章节课时
    public function deleteCourseSection(int $id, int $sid)
    {
        app(CourseSection::class)->deleteCourseSection($id, $sid);

        return $this->parseToData([], 1, '删除成功');
    }
}
